package com.yundingai.controller;

import com.yundingai.config.MinioConfig;
import com.yundingai.service.impl.FileAnalysisService;
import com.yundingai.service.impl.MinioService;
import com.yundingai.utils.MinIOUtil;
import com.yundingai.vm.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author HuangZhengmin
 * @desc
 * @Date 2025/7/16 14:54
 * @Version 1.0
 */

@RestController
@RequestMapping("/file")
@CrossOrigin
@Slf4j
public class MinIOController {

  @Autowired
  private MinioConfig minioConfig;
  @Autowired
  private MinIOUtil minIOUtil;
  @Autowired
  private MinioService minioService;

  @Autowired
  private FileAnalysisService fileAnalysisService;

  /**
   * 上传文件
   * @param file 上传的文件
   * @return map 包含文件名和url
   */
  @PostMapping("/upload")
  public ResponseResult upload(@RequestParam("file") MultipartFile file) {
    log.info("上传文件：{}", file.getOriginalFilename());
    Map<String, String> map = new HashMap<>();
    map.put("url", minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + minIOUtil.upload(file));
    map.put("fileName", minIOUtil.upload(file));
    return ResponseResult.okResult(map);
  }

  /**
   * 删除文件 通过文件名删除
   * @param fileName
   * @return
   */
  @DeleteMapping
  public ResponseResult delete(@RequestParam String fileName) {
    return ResponseResult.okResult(minIOUtil.remove(fileName));
  }
  /**
   * 分析Minio中的文件
   * @param objectName Minio中的对象名称
   * @return AI分析结果
   */
  @GetMapping("/analyze")
  public String analyzeFile(@RequestParam String objectName) {
    return minioService.analyzeFile(objectName);
  }

  /**
   * 上传并立即分析文件
   * @param file 上传的文件
   * @return AI分析结果
   */
  @PostMapping("/upload-and-analyze")
  public String uploadAndAnalyze(@RequestParam("file") MultipartFile file) {
    String objectName = minIOUtil.upload(file);
    return minioService.analyzeFile(objectName);
  }

  /**
   * 流式分析Minio中的文件
   * @param objectName Minio中的对象名称
   * @return ServerSentEvent流
   */
  @GetMapping(value = "/stream-analyze", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<ServerSentEvent<String>> streamAnalyzeFile(@RequestParam String objectName) {
    return minioService.streamAnalyzeFile(objectName);
  }

  /**
   * 上传并流式分析文件
   * @param file 上传的文件
   * @return ServerSentEvent流
   */
  @PostMapping(value = "/upload-and-stream-analyze", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<ServerSentEvent<String>> uploadAndStreamAnalyze(@RequestParam("file") MultipartFile file) {
    String objectName = minIOUtil.upload(file);
    return minioService.streamAnalyzeFile(objectName);
  }

  /**
   * 流式处理文件（根据用户需求）
   */
  @GetMapping(value = "/stream-process", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<ServerSentEvent<String>> streamProcessFile(
          @RequestParam String objectName,
          @RequestParam String request) {

    log.info("开始处理文件: {}, 用户需求: {}", objectName, request);

    return minioService.streamProcessFile(objectName, request)
            .doOnSubscribe(s -> log.info("客户端已连接"))
            .doOnComplete(() -> log.info("流式处理完成"))
            .doOnError(e -> log.error("流式处理出错", e));
  }

  @GetMapping(value = "/chat-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<ServerSentEvent<String>> streamChat(@RequestParam String message) {
    return fileAnalysisService.streamChat(message);
  }

}